<?php


namespace Modules\Api\Controllers;

use Lib\Response;
use Models\Monitor\ScMonitorinfo;
use Phalcon\Paginator\Adapter\QueryBuilder;

class MonitorController extends ControllerBase
{
    // http://irsapi.dev/api/monitor/test
    // http://10.28.129.132:8081/api/monitor/test
    // https://10.28.129.132:8082/api/monitor/test
    public function testAction()
    {
        $result = '{"key":"value","key1":"value2","key2":"value2"}';
        return Response::data($result);
    }

    public function getoneinfoAction()
    {
        $request = $this->request;
        $jsonData = $request->getJsonRawBody();

        if ($request->isPost()) {
            $datetime   = date("Y-m-d H:i:s");
            $srcfileday = date("Y-m-d");

            if (empty($jsonData)) {
                return Response::error('信息不能为空');
            }

            $model = new ScMonitorinfo();
            $model->setCreateTime($datetime);
            $model->setSrcFileDay($srcfileday);
            $model->setBaseinfo(json_encode($jsonData));
            $model->setType(1);

            try {
                $result = $model->save();

                if ($result) {
                    return Response::data([], '信息保存成功');
                } else {
                    return Response::error('信息保存失败');
                }
            } catch (\Exception $exc) {
                return Response::error($exc->getMessage());
            }
        }

        return Response::error('请使用POST方式传值');
//        if (property_exists($jsonData, "instanceId")) {
//            $instanceId = $jsonData->instanceId;
//        } else {
//            return $this->response([], false, 100001, "资产实例唯一ID 不能为空");
//        }
//
//        if (property_exists($jsonData, "monitorTime")) {
//            $monitorTime = $jsonData->monitorTime;
//        } else {
//            return $this->response([], false, 100002, "监控时间-时间戳格式 不能为空");
//        }
//
//        if (property_exists($jsonData, "itemId")) {
//            $itemId = $jsonData->itemId;
//        } else {
//            return $this->response([], false, 100003, "监控指标 不能为空");
//        }
//
//        if (property_exists($jsonData, "itemName")) {
//            $itemName = $jsonData->itemName;
//        } else {
//            return $this->response([], false, 100004, "监控指标名称 不能为空");
//        }
//
//        if (property_exists($jsonData, "itemType")) {
//            $maxVal = $jsonData->itemType;
//        } else {
//            return $this->response([], false, 100005, "资产类型 不能为空");
//        }
//
//        if (property_exists($jsonData, "maxVal")) {
//            $maxVal = $jsonData->maxVal;
//        } else {
//            return $this->response([], false, 100006, "最大值 不能为空");
//        }
//
//        if (property_exists($jsonData, "minVal")) {
//            $minVal = $jsonData->minVal;
//        } else {
//            return $this->response([], false, 100007, "最小值 不能为空");
//        }
//
//        if (property_exists($jsonData, "avgVal")) {
//            $avgVal = $jsonData->avgVal;
//        } else {
//            return $this->response([], false, 100008, "平均值 不能为空");
//        }
//
//        if (property_exists($jsonData, "currentVal")) {
//            $currentVal = $jsonData->currentVal;
//        } else {
//            return $this->response([], false, 100009, "当前值 不能为空");
//        }
//
//        if (property_exists($jsonData, "childType")) {
//            $childType = $jsonData->childType;
//        } else {
//            return $this->response([], false, 100010, "二级分类 不能为空");
//        }
//
//        if (property_exists($jsonData, "childVal")) {
//            $childVal = $jsonData->childVal;
//        } else {
//            return $this->response([], false, 100011, "二级分类值 不能为空");
//        }
    }

    public function getbatchinfoAction()
    {
        $request = $this->request;
        $jsonData = $request->getJsonRawBody();

        if (count($jsonData) === 0) {
            return Response::error('信息不能为空');
        }

//        foreach ($jsonData as $jsonDatum) {
//
//        }

        if ($request->isPost()) {
            $datetime = date("Y-m-d H:i:s");
            $srcfileday = date("Y-m-d");

            if (empty($jsonData)) {
                return Response::error('信息不能为空');
            }

            $model = new ScMonitorinfo();
            $model->setCreateTime($datetime);
            $model->setSrcFileDay($srcfileday);
            $model->setBaseinfo(json_encode($jsonData));
            $model->setType(1);

            try {
                $result = $model->save();

                if ($result) {
                    return Response::data([], '信息保存成功');
                } else {
                    return Response::error('信息保存失败');
                }
            } catch (\Exception $exc) {
                return Response::error($exc->getMessage());
            }
        }

        return Response::error('请使用POST方式传值');
    }

    public function getdxoneinfoAction()
    {
        $request = $this->request;
        $jsonData = $request->getJsonRawBody();

        if ($request->isPost()) {
            $datetime   = date("Y-m-d H:i:s");
            $srcfileday = date("Y-m-d");

            if (empty($jsonData)) {
                return Response::error('信息不能为空');
            }

            $model = new ScMonitorinfo();
            $model->setCreateTime($datetime);
            $model->setSrcFileDay($srcfileday);
            $model->setBaseinfo(json_encode($jsonData));
            $model->setType(2);

            try {
                $result = $model->save();

                if ($result) {
                    return Response::data([], '信息保存成功');
                } else {
                    return Response::error('信息保存失败');
                }
            } catch (\Exception $exc) {
                return Response::error($exc->getMessage());
            }
        }

        return Response::error('请使用POST方式传值');
    }

    public function getdxbatchinfoAction()
    {
        $request = $this->request;
        $jsonData = $request->getJsonRawBody();

        if (count($jsonData) === 0) {
            return Response::error('信息不能为空');
        }

//        foreach ($jsonData as $jsonDatum) {
//
//        }

        if ($request->isPost()) {
            $datetime = date("Y-m-d H:i:s");
            $srcfileday = date("Y-m-d");

            if (empty($jsonData)) {
                return Response::error('信息不能为空');
            }

            $model = new ScMonitorinfo();
            $model->setCreateTime($datetime);
            $model->setSrcFileDay($srcfileday);
            $model->setBaseinfo(json_encode($jsonData));
            $model->setType(2);

            try {
                $result = $model->save();

                if ($result) {
                    return Response::data([], '信息保存成功');
                } else {
                    return Response::error('信息保存失败');
                }
            } catch (\Exception $exc) {
                return Response::error($exc->getMessage());
            }
        }

        return Response::error('请使用POST方式传值');
    }


//Monitor
//instanceId
//monitorTime
//itemId
//itemName
//itemType
//maxVal
//minVal
//avgVal
//currentVal
//childType
//childVal

}